#include "CGamePlayState.h"
#include "../CGame.h"
#include "CMainMenuState.h"
#include "..\Wrappers\CSGD_Direct3D.h"
#include "..\Wrappers\CSGD_DirectInput.h"
#include "..\Wrappers\CSGD_FModManager.h"
#include "..\Wrappers\CSGD_TextureManager.h"
#include "..\Game Objects\CEnemy.h"
#include "..\Messaging\DHD_Messages.h"
#include "..\Game Objects\CBase.h"
#include "..\Wrappers\DHD_ObjectFactory.h"
#include "..\Wrappers\DHD_ObjectManager.h"
#include "..\Game Objects\CPlayer.h"
#include "..\CCamera.h"
#include <string>
#include <time.h>
using std::string;

CGamePlayState::CGamePlayState() 
{
	DHD_EventSystem::GetInstance()->RegisterClient("DestroyAll", this);

}

void CGamePlayState::Enter()
{
	
	srand( (unsigned int) time(0));
	
	DHD_ObjectFactory<string, CBase>* pOF = DHD_ObjectFactory<string, CBase>::GetInstance();
	DHD_ObjectManager* pOM = DHD_ObjectManager::GetInstance();

	pOF->RegisterClassType<CBase>("CBase");
	pOF->RegisterClassType<CEnemy>("CEnemy");
	pOF->RegisterClassType<CPlayer>("CPlayer");

	DHD_MessageSystem::GetInstance()->InitMessageSystem(CGamePlayState::MessageProc);

	m_pPlayer = (CPlayer*)pOF->CreateObject("CPlayer");
	m_pPlayer->SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture("resource/Player.jpg"));
	m_pPlayer->SetHeight(32);
	m_pPlayer->SetWidth(32);
	m_pPlayer->SetPosX(CGame::GetInstance()->GetScnWidth()*.5);
	m_pPlayer->SetPosY(500);

	CCamera::GetInstance()->SetPosX(m_pPlayer->GetPosX());
	//CCamera::GetInstance()->SetPosX(m_pPlayer->GetPosX());

	pOM->AddObject(m_pPlayer);

}

bool CGamePlayState::Input()
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	
	if(pDI->KeyPressed(DIK_RETURN))
	{

		CGame::GetInstance()->ChangeState(CMainMenuState::GetInstance());

	}
	if( pDI->JoystickButtonPressed( 0 )|| pDI->KeyPressed(DIK_1))
	{
		buttonStack.push_back(1);
	}
	if( pDI->JoystickButtonPressed( 1 )||  pDI->KeyPressed(DIK_2))
	{
		buttonStack.push_back(2);
	}
	if( pDI->JoystickButtonPressed( 2 )||  pDI->KeyPressed(DIK_3))
	{
		buttonStack.push_back(3);
	}
	if( pDI->JoystickButtonPressed( 3 )||  pDI->KeyPressed(DIK_4))
	{
		buttonStack.push_back(4);
	}

	if(	pDI->KeyDown(DIK_LEFT) || pDI->JoystickDPadDown(DIR_LEFT))
	{
		//	m_pPlayer->SetPosX(m_pPlayer->GetPosX() - 0.25);
		CCamera::GetInstance()->SetPosX(CCamera::GetInstance()->GetPosX() +0.25);
	}

	if(	pDI->KeyDown(DIK_RIGHT) || pDI->JoystickDPadDown(DIR_RIGHT))
	{
		//m_pPlayer->SetPosX(m_pPlayer->GetPosX() + 0.25);
		CCamera::GetInstance()->SetPosX(CCamera::GetInstance()->GetPosX() -0.25);

	}


	if(pDI->KeyPressed(DIK_B)  || pDI->JoystickButtonPressed(8)) 
	{
		DHD_MessageSystem::GetInstance()->SendMsg(new CCreateEnemyMessage());
	}

	return true;

}

void CGamePlayState::Update(float _fDT)
{

	DHD_ObjectManager::GetInstance()->UpdateObjects(_fDT);

	if( buttonStack.size() > 4 )
	{
		buttonStack.clear();
	}

	DHD_EventSystem::GetInstance()->ProcessEvents();
	DHD_MessageSystem::GetInstance()->ProcessMessages();
	

}
void CGamePlayState::Render(void)
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pD3D->Clear(255, 255, 255);
	pD3D->DeviceBegin();
	pD3D->SpriteBegin();

	//pTM->Draw(m_nCursorID, 1, m_nCursorIndex*20 + 30);




	pD3D->DrawTextA("gameplaystate", 20, 10);
	
	if( buttonStack.size() == 4)
	{
		for( int i=0; i <buttonStack.size(); i++ )
		{
			char combo[100];

			sprintf(combo, "<%i %i %i %i>", buttonStack[0],buttonStack[1],buttonStack[2],buttonStack[3]);

			CSGD_Direct3D::GetInstance()->DrawTextA(combo, 400,200,0,0,0);
			//pD3D->DrawTextA(buttonStack[i],400+i*20,100,255,0,0);
		}
	}

	pD3D->GetSprite()->Flush();

	DHD_ObjectManager::GetInstance()->RenderObjects();

	pD3D->DeviceEnd();
	pD3D->SpriteEnd();

	pD3D->Present();



}
void CGamePlayState::Exit(void)
{

	DHD_ObjectFactory<string, CBase>* pOF = DHD_ObjectFactory<string, CBase>::GetInstance();
	pOF->UnregisterClassType("CBase");
	pOF->UnregisterClassType("CEnemy");
	pOF->UnregisterClassType("CPlayer");
	DHD_MessageSystem::GetInstance()->ShutdownMessageSystem();

	pOF->ShutdownObjectFactory();

	DHD_MessageSystem::GetInstance()->ShutdownMessageSystem();
	DHD_EventSystem::GetInstance()->ShutdownEventSystem();
}

void CGamePlayState::MessageProc( CBaseMessage* pMSG )
{
	switch( pMSG->GetMsgID() )
	{
		

	case MSG_CREATE_ENEMY:
		{
			CGamePlayState* pCGP = CGamePlayState::GetInstance();
			CCreateEnemyMessage* pCTM = (CCreateEnemyMessage*)(pMSG);
			DHD_ObjectFactory<string, CBase>* pOF = DHD_ObjectFactory<string, CBase>::GetInstance();
			CEnemy* pEnemy = (CEnemy*)pOF->CreateObject("CEnemy");
			pEnemy->SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture("resource/ICON.png"));
			pEnemy->SetPosX(rand() % (800));
			pEnemy->SetPosY(500);
			pEnemy->SetHeight(32);
			pEnemy->SetWidth(32);
			pEnemy->SetComboSize(4);
			pEnemy->GenerateCombo();
			pEnemy->SetTarget(CGamePlayState::GetInstance()->GetPlayer());
			DHD_ObjectManager::GetInstance()->AddObject(pEnemy);
			pEnemy->Release();

		}
		break;
	
	case MSG_DESTROY_ALL:
		{

			DHD_ObjectManager::GetInstance()->RemoveAllObjects();
		}
		break;
	case MSG_DESTROY_ENEMY:
		{
			CDestroyEnemyMessage* pDBM = (CDestroyEnemyMessage*)pMSG;

			CEnemy* pEne = pDBM->GetEnemy();

			DHD_ObjectManager::GetInstance()->RemoveObject( pEne );
		}
	}	
}


void CGamePlayState::HandleEvent(CEvent* pEvent)
{
	if( pEvent->GetEventID() == "DestroyAll")
	{
		DHD_MessageSystem::GetInstance()->SendMsg(new DestroyAllEnemies());
	}

}

